package com.fx.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * des:
 *
 * @author fxiao
 * @date 2020/12/11 13:02
 */
@Entity(name = "t_resource")
public class Resource{
    public Resource(String name, String code,Role ... roles) {
        this.name = name;
        this.code = code;
        this.roles=new ArrayList<>(Arrays.asList(roles));
    }
    public Resource(String name, String code) {
        this.name = name;
        this.code = code;
    }
    public Resource() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    /**资源名*/
    private String name;
    /**资源符*/
    @Column(unique = true)
    private String code;
    /**父资源code*/
    @ManyToOne
    @JoinColumn
    private Resource parent;
    /**上层所有父资源，用“/”分隔*/
    private String parents;

    @ManyToMany(fetch = FetchType.LAZY,cascade=CascadeType.REFRESH)
    @JoinTable(
            name = "roleResource"
            ,joinColumns = {@JoinColumn(name = "resourceId")}
            ,inverseJoinColumns = {@JoinColumn(name = "roleId")}
    )
    @JsonIgnore
    private List<Role> roles;

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Resource getParent() {
        return parent;
    }

    public void setParent(Resource parent) {
        this.parent = parent;
    }

    public String getParents() {
        return parents;
    }

    public void setParents(String parents) {
        this.parents = parents;
    }


}
